/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.navigator;

import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.terminal.Resource;
import org.processbase.touchprocess.ui.TouchProcessApplication;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Table;
import com.vaadin.ui.themes.BaseTheme;
import org.processbase.touchprocess.ui.demo.BarChartDemo;
import org.processbase.touchprocess.ui.demo.PieChartDemo;
import org.processbase.touchprocess.ui.template.ChartButton;
import org.processbase.touchprocess.ui.template.NavigationList;

public class DashboardNavigationView extends NavigationList {

    private Resource iconPie = new ThemeResource("images/chart_pie.png");
    private Resource iconBar = new ThemeResource("images/chart_bar.png");

    @Override
    public void initUI() {
        super.initUI();
        table.addContainerProperty("type", Embedded.class, null);
        table.setColumnWidth("type", 48);
        table.setColumnAlignment("type", Table.ALIGN_RIGHT);
        table.addContainerProperty("chart", ChartButton.class, null);
        table.setVisibleColumns(new Object[]{"type", "chart"});
        table.setColumnExpandRatio("chart", 1);

        Item item = table.addItem("bar");
        ChartButton btn = new ChartButton("BarChart demo");
        btn.addListener(new LayoutClickListener() {

            public void layoutClick(LayoutClickEvent event) {
                table.setValue("bar");
            }
        });
        item.getItemProperty("chart").setValue(btn);
        Button btnIcon = new Button(null);
        btnIcon.setStyleName(BaseTheme.BUTTON_LINK);
        btnIcon.setIcon(iconBar);
        item.getItemProperty("type").setValue(new Embedded(null, iconBar));

        Item item2 = table.addItem("pie");
        ChartButton btn2 = new ChartButton("PieChart demo");
        btn2.addListener(new LayoutClickListener() {

            public void layoutClick(LayoutClickEvent event) {
                table.setValue("pie");
            }
        });
        item2.getItemProperty("chart").setValue(btn2);
        Button btnIcon2 = new Button(null);
        btnIcon2.setStyleName(BaseTheme.BUTTON_LINK);
        btnIcon2.setIcon(iconPie);
        item2.getItemProperty("type").setValue(new Embedded(null, iconPie));

        setCaption("Demo charts (2)");


        table.addListener(new ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {
                if (event.getProperty().getValue().equals("bar")) {
                    BarChartDemo barChartDemo = new BarChartDemo();
                    TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getDetailsView().setDetailsLayout(barChartDemo);
//                    barChartDemo.initUI();
                } else if (event.getProperty().getValue().equals("pie")) {
                    PieChartDemo pieChartDemo = new PieChartDemo();
                    TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getDetailsView().setDetailsLayout(pieChartDemo);
//                    pieChartDemo.initUI();
                }
            }
        });
    }
}